let app = getApp()
const util = require('../../../utils/util.js')

Page({
    /**
     * 页面的初始数据
     */
    data: {
        enablePullDownRefresh: false,
        spinShow: false,
        loadMoreLoad: false,
        loadMoreTip: '',
        queryParam: {
            pageNum: 1,
            pageSize: app.globalData.pageSize
        },
        tableData: [],
        pages: 0,

        option1: [
            {text: '全部', value: ''},
            {text: '已出库', value: 1},
            {text: '已撤销', value: 9},
        ],
        //搜索字段
        searchVal: "",
        startDate: "",
        endDate: "",
        //状态
        status: '',
        orderId: '',

    },

    showPopup() {
    },
    onPullDownRefresh() {
        this.setData({
            spinShow: true
        });
        if (!this.loading) {
            this.setData({
                ['queryParam.pageNum']: 1,
                loadMoreLoad: false,
                loadMoreTip: '正在刷新',
            });
            this.queryData(true)
        }
    },
    onReachBottom() {
        if (!this.loading && this.data.queryParam.pageNum < this.data.pages) {
            this.setData({
                loadMoreLoad: true,
                loadMoreTip: '正在加载'
            });
            this.setData({
                ['queryParam.pageNum']: this.data.queryParam.pageNum + 1
            });
            this.queryData(false)
        }
    },
    queryData: function (override) {
        let _this = this
        let data = this.data.queryParam
        data.searchVal = this.data.searchVal
        if (this.data.startDate && this.data.startDate != "") {
            data.startDate = this.data.startDate + " 00:00:00"
        } else {
            data.startDate = ''
        }
        if (this.data.endDate && this.data.endDate != "") {
            data.endDate = this.data.endDate + " 23:59:59"
        } else {
            data.endDate = ''
        }
        data.orderId = this.data.orderId
        data.status = this.data.status
        app.formGet('/app/out/store/page', data).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                let tableData = override ? res.data.list : this.data.tableData.concat(res.data.list)
                _this.setData({
                    tableData: tableData,
                    pages: res.data.pageInfo.pages
                });
                if (this.data.queryParam.pageNum >= res.data.pageInfo.pages) {
                    this.setData({
                        loadMoreLoad: false,
                        loadMoreTip: '暂无更多数据'
                    });
                }
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let orderId = options.orderId
        if (orderId) {
            this.data.orderId = orderId
        }
    },
    onShow: function () {
        let _this = this
        this.setData({
            spinShow: true,
            queryParam: {
                pageNum: 1,
                pageSize: app.globalData.pageSize
            },
            loadMoreLoad: false,
            loadMoreTip: ''
        });
        this.queryData(true)
    },
    // dropdown
    onChange() {
        //切换之后，分页重置
        this.data.queryParam.pageNum = 1
        this.queryData(true)
    },
    //确认搜索
    onConfirm() {
        this.data.queryParam.pageNum = 1
        this.selectComponent('#item').toggle()
        this.queryData(true)
    },
    //清空搜索项
    onReset: function () {
        this.setData({
            searchVal: "",
            startDate: "",
            endDate: "",
        })
        this.onConfirm()
    },
    //悬浮加号点击
    plusClick: function () {
        wx.navigateTo({
            url: "/pages/order/add/index"
        })
    },
    onStartDateConfirm: function (e) {
        this.setData({
            startDate: util.formatDate(new Date(e.detail)),
        });
    },
    onEndDateConfirm: function (e) {
        this.setData({
            endDate: util.formatDate(new Date(e.detail)),
        });
    }
})
